<?php 
class ControllerCategory extends Controller {
	var $error = array();
 
	function index() {
		$response =& $this->locator->get('response');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$module   =& $this->locator->get('module');

		$language->load('controller/category.php');

		$template->set('title', $language->get('heading_title'));
		
		$template->set('content', $this->getList());

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}

	function insert() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$database =& $this->locator->get('database');
		$upload   =& $this->locator->get('upload');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$session  =& $this->locator->get('session');
		$module   =& $this->locator->get('module');
		$cache    =& $this->locator->get('cache');
		$image    =& $this->locator->get('image');

		$language->load('controller/category.php');

		$template->set('title', $language->get('heading_title'));

		$image->checkFiles(); //checks for new files in the image folder to ensure they are available
		
		if ($request->isPost() && $request->has('image_id', 'post') && $this->validateForm() ) {
			
			// Add image upload to category edit
	  		if ($upload->has('image') && $this->validateUpload() && $upload->save('image', DIR_IMAGE . $upload->getName('image'))) {
				$sql = "insert into image set filename = '?', date_added = now()";
				$database->query($database->parse($sql, $upload->getName('image')));
      		    $image_id = $database->getLastId();
      		    
      		    //Image title forced to image name for all languages
        		foreach ($request->get('language', 'post', array()) as $key => $value) {
        			$sql = "insert into image_description set image_id = '?', language_id = '?', title = '?'";
        			$database->query($database->parse($sql, $image_id, $key, $image->getTitle($upload->getName('image'))));
				}
				$cache->delete('image');
			} else {
				$image_id = $request->gethtml('image_id', 'post');
			}
			
			$sql = "insert into category set image_id = '?', sort_order = '?', parent_id = '?', date_added = now(), status = '?'";
			$database->query($database->parse($sql, $image_id, $request->gethtml('sort_order', 'post'), end(explode('_', $request->gethtml('path'))), $request->gethtml('status', 'post')));

			$insert_id = $database->getLastId();

			if ($request->gethtml('path')) {
				$path = $request->gethtml('path') . '_' . $insert_id;
			} else {
				$path = $insert_id;
			}

			$sql = "update category set path = '?' where category_id = '?'";
			$database->query($database->parse($sql, $path, $insert_id));

			$description = $request->gethtml('description', 'post');

			foreach ($request->gethtml('language', 'post') as $key => $value) {
				$sql = "insert into category_description set category_id = '?', language_id = '?', name = '?', description = '?'";
				$database->query($database->parse($sql, $insert_id, $key, $value['name'], html_entity_decode($description[$key])));
			}

			$cache->delete('category');
			
			$session->set('message', $language->get('text_message'));

			$response->redirect($url->ssl('category', FALSE, array('path' => $request->gethtml('path'))));
		}

		$template->set('content', $this->getForm());

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}

	function update() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$database =& $this->locator->get('database');
		$upload   =& $this->locator->get('upload');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$session  =& $this->locator->get('session');
		$module   =& $this->locator->get('module');
		$cache    =& $this->locator->get('cache');
		$image    =& $this->locator->get('image');

		$language->load('controller/category.php');

		$template->set('title', $language->get('heading_title'));

		$image->checkFiles(); //checks for new files in the image folder to ensure they are available
		
		if ($request->isPost() && $request->has('image_id', 'post') && $this->validateForm()) {
			
			// Add image upload to category edit
	  		if ($upload->has('image') && $this->validateUpload() && $upload->save('image', DIR_IMAGE . $upload->getName('image'))) {
				$sql = "insert into image set filename = '?', date_added = now()";
				$database->query($database->parse($sql, $upload->getName('image')));
      		    $image_id = $database->getLastId();
      		    
      		    //Image title forced to image name for all languages
        		foreach ($request->get('language', 'post', array()) as $key => $value) {
        			$sql = "insert into image_description set image_id = '?', language_id = '?', title = '?'";
        			$database->query($database->parse($sql, $image_id, $key, $image->getTitle($upload->getName('image'))));
				}
				$cache->delete('image');
			} else {
				$image_id = $request->gethtml('image_id', 'post');
			}
			
			$sql = "update category set image_id = '?', sort_order = '?', date_modified = now(), status = '?' where category_id = '?'";
			$database->query($database->parse($sql, $image_id, $request->gethtml('sort_order', 'post'), $request->gethtml('status', 'post'), $request->gethtml('category_id')));

			$database->query("delete from category_description where category_id = '" . (int)$request->gethtml('category_id') . "'");

			$description = $request->gethtml('description', 'post');

			foreach ($request->gethtml('language', 'post') as $key => $value) {
				$sql = "insert into category_description set category_id = '?', language_id = '?', name = '?', description = '?'";
				$database->query($database->parse($sql, $request->gethtml('category_id'), $key, $value['name'], html_entity_decode($description[$key])));
			}

			$cache->delete('category');
			
			$session->set('message', $language->get('text_message'));

			$response->redirect($url->ssl('category', FALSE, array('path' => $request->gethtml('path'))));
		}

		$template->set('content', $this->getForm());

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}

	function listAction() {
    	$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$database =& $this->locator->get('database');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$session  =& $this->locator->get('session');
		$module   =& $this->locator->get('module');
		$cache    =& $this->locator->get('cache');

    	$language->load('controller/category.php');

    	$template->set('title', $language->get('heading_title'));
	
    	if (($request->has('listitem', 'post')) && ($this->validateAction())) {
      		foreach ($request->gethtml('listitem', 'post') as $category_id) {
      			if ($request->gethtml('listcmd', 'post') == $language->get('select_delete')) {
      				$database->query("delete from category where category_id = '" . $category_id . "'");
					$database->query("delete from category_description where category_id = '" . $category_id . "'");

					if ($request->gethtml('path')) {
						$path = $request->gethtml('path') . '_' . $category_id;
					} else {
						$path = $category_id;
					}

					$sql = "delete category, category_description from category left join category_description on category.category_id = category_description.category_id where category.path like '?'";
					$database->query($database->parse($sql, $path . '%'));
			
					$session->set('message', $language->get('text_delete_message'));
					
	  			} elseif ($request->gethtml('listcmd', 'post') == $language->get('select_toggle')) { 
					$result = $database->getRow("select status from category where `category_id` = '" . $category_id . "'"); 
					$sql = "update category set status = '?' where category_id = '?'";
      				$database->query($database->parse($sql, ($result['status'] == 1) ? 0 : 1, $category_id));
					$session->set('message', $language->get('text_status_message'));
						
				} elseif ($request->gethtml('listcmd', 'post') == $language->get('select_clone')) { 
					
					// Clone category table
		            $row = $database->getRow("select * from category where `category_id` = '" . $category_id . "'");
		            $row['category_id'] = NULL;
		            $result = $database->query("insert into category (" . implode(", ", array_keys($row)). ") VALUES ('" . implode("', '", array_values($row)) . "')");
		            
		            // Get new category_id
		            $insert_id = $database->getLastId();
		            
		            if ($request->gethtml('path')) {
		                $pos = strrpos($request->gethtml('path'), '_');
		                $path = substr($request->gethtml('path'), 0, $pos) . $insert_id;
		            } else {
		                $path = $insert_id;
		            }

		            $sql = "update category set path = '?' where category_id = '?'";
		            $database->query($database->parse($sql, $path, $insert_id));
		            
		            // Clone category_description table for each language
		            $rows = $database->getRows("select * from category_description where `category_id` = '" . $category_id . "'");
		            foreach ($rows as $row) {
		                $row['category_id'] = $insert_id;
		                $i = $database->countRows("select name from `category_description` where `name` LIKE '" . addslashes($row['name']) . "%'");
		                $row['name'] = addslashes($row['name']) . $language->get('text_clone_sfx', $i);
		                if (isset($row['description'])) {
		                    $row['description'] = addslashes($row['description']);
		                }
		                $result = $database->query("insert into category_description (" . implode(", ", array_keys($row)). ") VALUES ('" . implode("', '", array_values($row)) . "')");
		            }
		            
		            $session->set('message', $language->get('text_clone_message'));
				}
			}
			
	  		$cache->delete('category');
	  		
	  		$response->redirect($url->ssl('category', FALSE, array('path' => $request->gethtml('path'))));
    	}
    
    	$template->set('content', $this->getList());
	
		$template->set($module->fetch());
	
    	$response->set($template->fetch('layout.tpl'));
  	}

  	function delete() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$database =& $this->locator->get('database');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$session  =& $this->locator->get('session');
		$module   =& $this->locator->get('module');
		$cache    =& $this->locator->get('cache');

		$language->load('controller/category.php');

		$template->set('title', $language->get('heading_title'));

		if (($request->gethtml('category_id')) && ($this->validateDelete())) {
			$database->query("delete from category where category_id = '" . (int)$request->gethtml('category_id') . "'");
			$database->query("delete from category_description where category_id = '" . (int)$request->gethtml('category_id') . "'");

			if ($request->gethtml('path')) {
				$path = $request->gethtml('path') . '_' . $request->gethtml('category_id');
			} else {
				$path = $request->gethtml('category_id');
			}

			$sql = "delete category, category_description from category left join category_description on category.category_id = category_description.category_id where category.path like '?'";
			$database->query($database->parse($sql, $path . '%'));

			$cache->delete('category');
			
			$session->set('message', $language->get('text_message'));

			$response->redirect($url->ssl('category', FALSE, array('path' => $request->gethtml('path'))));
		}

		$template->set('content', $this->getList());

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}
  	
	function getList() {
		$request  =& $this->locator->get('request');
		$database =& $this->locator->get('database');
		$config   =& $this->locator->get('config');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$session  =& $this->locator->get('session');
		$image	  =& $this->locator->get('image');
		$template =& $this->locator->get('template');

		$cols = array();

		$cols[] = array(
            'name'  => $language->get('column_id'),
            'sort'  => 'c.category_id',
            'href'  => $url->ssl('category', 'page', array('sort' => 'c.category_id', 'order' => ($request->gethtml('order') == 'desc') ? 'asc' : 'desc')),
            'align' => 'left'
        );
		
		$cols[] = array(
			'name'  => $language->get('column_name'),
			'sort'  => 'cd.name',
			'href'  => $url->ssl('category', 'page', array('sort' => 'cd.name', 'order' => ($request->gethtml('order') == 'desc') ? 'asc' : 'desc')),
			'align' => 'left'
		);

		$cols[] = array(
			'name'  => $language->get('column_sort_order'),
			'sort'  => 'c.sort_order',
			'href'  => $url->ssl('category', 'page', array('sort' => 'c.sort_order', 'order' => ($request->gethtml('order') == 'desc') ? 'asc' : 'desc')),
			'align' => 'center'
		);
    	
    	$cols[] = array(
      		'name'  => $language->get('column_image'),
      		'align' => 'right'
    	);
    	
		$cols[] = array(
      		'name'  => $language->get('column_action'),
      		'align' => 'right'
    	);
    	
    	$cols[] = array(
        	'name' => '<input type="checkbox" name="checkall" onclick="$(\'input[name*=listitem]\').attr(\'checked\', this.checked);" />',
            'align' => 'right'
        );
		
		if ((!$session->has('category.search')) || ($request->gethtml('path'))) {
			$sql = "select * from category c left join category_description cd on c.category_id = cd.category_id where c.parent_id = '" . (int)end(explode('_', $request->gethtml('path'))) . "' and language_id = '" . (int)$language->getId() . "'";
		} else {
			$sql = "select * from category c left join category_description cd on c.category_id = cd.category_id where language_id = '" . (int)$language->getId() . "' and cd.name like '?'";
		}

		$sort = array(
			'c.category_id',
			'cd.name',
			'c.sort_order'
		);

		if (in_array($session->get('category.sort'), $sort)) {
			$sql .= " order by " . $session->get('category.sort') . " " . (($session->get('category.order') == 'desc') ? 'desc' : 'asc');
		} else {
			$sql .= " order by cd.name asc";
		}

		$results = $database->getRows($database->splitQuery($database->parse($sql, '%' . $session->get('category.search') . '%'), ($request->has('path') ? $session->get('category.' . $request->gethtml('path') . '.page') : $session->get('category.page')), $config->get('config_max_rows')));

		$rows = array();

		foreach ($results as $result) {
			$cell = array();

			if ($request->gethtml('path')) {
				$path = $request->gethtml('path') . '_' . $result['category_id'];
			} else {
				$path = $result['category_id'];
			}

			$cell[] = array(
            	'value' => $path,
            	'align' => 'left'
        	);
			
			$cell[] = array(
				'value' => $result['name'],
				'align' => 'left'
			);

			$cell[] = array(
				'value' => $result['sort_order'],
				'align' => 'center'
			);

			$img = $database->getRow("select filename from image where image_id = '".$result['image_id']."'");
			$cell[] = array(
				'image' => $image->resize($img['filename'], '22', '22'),
				'align' => 'right'
			);
			
			$query = array(
				'category_id' => $result['category_id'],
				'path'        => $request->gethtml('path'),
			);
			
			$action = array();
      		
			$action[] = array(
        		'icon' => 'update.png',
				'text' => $language->get('button_update'),
				'href' => $url->ssl('category', 'update', $query)
      		);
						
			$action[] = array(
        		'icon' => 'folder.png',
				'text' => $language->get('button_subcat'),
				'href' => $url->ssl('category', FALSE, array('path' => $path))
      		);
      		
      		$action[] = array(
        		'icon' => ($result['status'] ? 'enabled.png' : 'disabled.png'),
				'text' => $language->get('button_status'),
				'href' => $url->ssl('category', 'toggleStatus', array('category_id' => $result['category_id']))
      		);
      		
      		$action[] = array(
        		'icon' => 'delete.png',
				'text' => $language->get('button_delete'),
				'href' => $url->ssl('category', 'delete', $query)
      		);

      		$cell[] = array(
        		'action' => $action,
        		'align'  => 'right'
      		);
			
			$cell[] = array(
            	'value' => '<input type="checkbox" name="listitem[]" value="' . $result['category_id'] . '" />',
            	'align' => 'right'
        	);
			
			$rows[] = array('cell' => $cell);
		}

		$view = $this->locator->create('template');

		$view->set('tpl', $template);

		$view->set('heading_title', $language->get('heading_title'));
		$view->set('heading_description', $language->get('heading_description'));

		$view->set('text_previous', $language->get('text_previous'));
		$view->set('text_results', $language->get('text_results', $database->getFrom(), $database->getTo(), $database->getTotal()));
		$view->set('select_clone', $language->get('select_clone'));
		$view->set('select_toggle', $language->get('select_toggle'));
		$view->set('select_delete', $language->get('select_delete'));
		
		$view->set('entry_page', $language->get('entry_page'));
		$view->set('entry_search', $language->get('entry_search'));

		$view->set('button_insert', $language->get('button_insert'));
		$view->set('button_update', $language->get('button_update'));
		$view->set('button_delete', $language->get('button_delete'));
		$view->set('button_subcats', $language->get('button_subcats'));

		$view->set('insert', $url->ssl('category', 'insert', array('path' => $request->gethtml('path'))));
		
		$view->set('error', @$this->error['message']);
		
		$view->set('message', $session->get('message'));
		
		$session->delete('message');
		
		$view->set('listaction', $url->ssl('category', 'listAction', array('path' => $request->gethtml('path'))));//Q
		
		$view->set('colspan', count($cols));
		
		$view->set('selectbox', array($language->get('select_clone'), $language->get('select_toggle'), $language->get('select_delete')));//Q
		
		$view->set('action', $url->ssl('category', 'page', array('path' => $request->gethtml('path'))));

		if ($request->gethtml('path')) {
			$path = explode('_', $request->gethtml('path'));

			if (count($path) > 1) {
				array_pop($path);

				$view->set('previous', $url->ssl('category', FALSE, array('path' => implode('_', $path))));
			} else {
				$view->set('previous', $url->ssl('category'));
			}
		} 

		$view->set('search', $session->get('category.search'));
		$session->delete('category.search');
		$view->set('sort', $session->get('category.sort'));
		$view->set('order', $session->get('category.order'));
		$view->set('page', ($request->has('path') ? $session->get('category.' . $request->gethtml('path') . 'page') : $session->get('category.page')));

		$view->set('cols', $cols);
		$view->set('rows', $rows);

		$page_data = array();

		for ($i = 1; $i <= $database->getPages(); $i++) {
			$page_data[] = array(
				'text'  => $language->get('text_pages', $i, $database->getPages()),
				'value' => $i
			);
		}

		$view->set('pages', $page_data);

		return $view->fetch('content/list.tpl');
	}

	function getForm() {
		$request  =& $this->locator->get('request');
		$database =& $this->locator->get('database');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');

		$view = $this->locator->create('template');

		$view->set('tpl', $template);

		$view->set('heading_title', $language->get('heading_title'));
		$view->set('heading_description', $language->get('heading_description'));

		$view->set('text_or', $language->get('text_or'));
		$view->set('text_enabled', $language->get('text_enabled'));
    	$view->set('text_disabled', $language->get('text_disabled'));
    	
    	$view->set('entry_status', $language->get('entry_status'));
        $view->set('entry_name', $language->get('entry_name'));
        $view->set('entry_description', $language->get('entry_description'));
		$view->set('entry_sort_order', $language->get('entry_sort_order'));
		$view->set('entry_image', $language->get('entry_image'));
		$view->set('entry_upload', $language->get('entry_upload'));

		$view->set('button_save', $language->get('button_save'));
		$view->set('button_cancel', $language->get('button_cancel'));

		$view->set('tab_general', $language->get('tab_general'));
		$view->set('tab_data', $language->get('tab_data'));
		$view->set('tab_image', $language->get('tab_image'));

		$view->set('error_description', @$this->error['message']); // not sure why just yet

        $view->set('error', @$this->error['message']);
        $view->set('error_upload', @$this->error['upload']);
		$view->set('error_name', @$this->error['name']);

		$query = array(
			'category_id' => $request->gethtml('category_id'),
			'path'        => $request->gethtml('path')
		);

		$view->set('action', $url->ssl('category', $request->gethtml('action'), $query));

		if ($request->gethtml('category_id')) {
			$query = array(
				'category_id' => $request->gethtml('category_id'),
				'path'        => $request->gethtml('path')
			);
		}

		$view->set('cancel', $url->ssl('category', FALSE, array('path' => $request->gethtml('path'))));
				
		$category_data = array();
		
		$results = $database->cache('language', "select * from language order by sort_order");

		foreach ($results as $result) {
			if (($request->gethtml('category_id')) && (!$request->isPost())) {
				$category_description_info = $database->getRow("select name, description from category_description where category_id = '" . (int)$request->gethtml('category_id') . "' and language_id = '" . (int)$result['language_id'] . "'");
			} else {
				$category_description_info = $request->gethtml('language', 'post');
			}
			
			$category_data[] = array(
				'language_id' => $result['language_id'],
				'language'    => $result['name'],
	    		'name'        => (isset($category_description_info[$result['language_id']]) ? $category_description_info[$result['language_id']]['name'] : @$category_description_info['name']),
                'description' => (isset($description[$result['language_id']]) ? $description[$result['language_id']] : @$category_description_info['description'])
			);
		}

		$view->set('categories', $category_data);

		if (($request->gethtml('category_id')) && (! $request->isPost())) {
			$category_info = $database->getRow("select distinct * from category where category_id = '" . (int)$request->gethtml('category_id') . "'");
		}

		if ($request->has('status', 'post')) {
      		$view->set('status', $request->gethtml('status', 'post'));
    	} elseif (isset($category_info['status'])) {
    		$view->set('status', @$category_info['status']);
    	} else {
      		$view->set('status', '1');
    	}
		
		if ($request->has('sort_order', 'post')) {
			$view->set('sort_order', $request->gethtml('sort_order', 'post'));
		} else {
			$view->set('sort_order', @$category_info['sort_order']);
		}

		if ($request->has('image_id', 'post')) {
			$view->set('image_id', $request->gethtml('image_id', 'post'));
		} else {
			$view->set('image_id', @$category_info['image_id']);
		}

		$image_data = array();

		$results = $database->cache('image', "select i.image_id, i.filename, id.title from image i left join image_description id on i.image_id = id.image_id where id.language_id = '" . (int)$language->getId() . "'");

		foreach ($results as $result) {
			$image_data[] = array(
				'image_id' => $result['image_id'],
				'title'    => $result['title']
			);
		}

		$view->set('images', $image_data);

		$editor = $this->locator->create('template');
		$view->set('editor',$editor->fetch('content/editor.tpl'));

		return $view->fetch('content/category.tpl');
	}

	function validateForm() {
		$request  =& $this->locator->get('request');
		$language =& $this->locator->get('language');
		$user     =& $this->locator->get('user');
		$validate =& $this->locator->get('validate');

		if (!$user->hasPermission('modify', 'category')) {
			$this->error['message'] = $language->get('error_permission');
		}

		foreach ($request->gethtml('language', 'post') as $value) {
			if (!$validate->strlen($value['name'],1,255)) {
				$this->error['name'] = $language->get('error_name');
			}
		}

		if (!$this->validateUpload()) {
			return FALSE;
		}
		
		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	function validateAction() {
		$language =& $this->locator->get('language');
		$user     =& $this->locator->get('user');  
    
    	if (!$user->hasPermission('modify', 'category')) {
      		$this->error['message'] = $language->get('error_permission');  
    	}
	  	
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
  	}
  	
  	function validateDelete() {
		$request  =& $this->locator->get('request');
		$language =& $this->locator->get('language');
		$user     =& $this->locator->get('user');

		if (!$user->hasPermission('modify', 'category')) {
			$this->error['message'] = $language->get('error_permission');
		}
 
		if (!$this->error) {
			return TRUE; 
		} else {
			return FALSE;
		}
	}
  	
  	function validateUpload() {
		$allowed = array(
			'image/jpeg',
			'image/pjpeg',
			'image/gif', 
			'image/png',
			'image/x-png'
		);

		$request  =& $this->locator->get('request');
		$language =& $this->locator->get('language');
		$user     =& $this->locator->get('user');
		$upload   =& $this->locator->get('upload');
        $validate =& $this->locator->get('validate');
        $image    =& $this->locator->get('image');

		if ($upload->has('image'))  {

	  		if ($upload->hasError('image')) {
	    		$this->error['upload'] = $language->get('error_upload');
	  		}

	  		if (!$validate->strlen($upload->getName('image'),1,128)) {
        		$this->error['upload'] = $language->get('error_filename');
			}
      
	    	if (!in_array($upload->getType('image'), $allowed)) {
          		$this->error['upload'] = $language->get('error_filetype');
        	}
	  
			if ($upload->hasError('image')) {
				$this->error['message'] = $upload->getError('image');
			}
    	}
		
		if (!$this->error) {
	  		return TRUE;
		}
  	}
		
  	function page() {
    	$request  =& $this->locator->get('request');
    	$response =& $this->locator->get('response');
		$url      =& $this->locator->get('url');
    	$session  =& $this->locator->get('session');	
	
		$session->delete('category.search');
		
		if ($request->has('search', 'post') && $request->gethtml('search', 'post') != '') {
	  		$session->set('category.search', $request->gethtml('search', 'post'));
		}
			
		if (($request->has('page', 'post')) || ($request->has('search', 'post'))) {
			$session->set(($request->has('path') ? 'category.' . $request->gethtml('path') . '.page' : 'category.page'), $request->gethtml('page', 'post'));
		}
			
		if ($request->has('order')) {
			$session->set('category.order', (($session->get('category.sort') == $request->gethtml('sort')) && ($session->get('category.order') == 'asc') ? 'desc' : 'asc'));
		}

		if ($request->has('sort')) {
			$session->set('category.sort', $request->gethtml('sort'));
		} 
		
		$response->redirect($url->ssl('category', FALSE, array('path' => $request->gethtml('path'))));
  	} 	
  	
  	function toggleStatus() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$database =& $this->locator->get('database');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$session  =& $this->locator->get('session');
		$module   =& $this->locator->get('module');
		$cache    =& $this->locator->get('cache');
		
		$language->load('controller/category.php');
		
		$result = $database->getRow("select status from category where `category_id` = '" . $request->gethtml('category_id') . "'"); 
		$sql = "update category set status = '?' where category_id = '?'";
      	$database->query($database->parse($sql, ($result['status'] == 1) ? 0 : 1, $request->gethtml('category_id')));
		
		$cache->delete('category');
	  		
		$session->set('message', $language->get('text_status_message'));
		$response->redirect($url->ssl('category'));     		
		
	}
}
?>
